from os import name
import urllib.request

'''练习1---URL的内容读取---'''

# url = 'http://www.baidu.com'
 
# response = urllib.request.urlopen(url=url)

# print(response.read().decode('utf8'))
# print(response.geturl())
# print(dict(response.getheaders()))
# print(response.getcode())
# print(response.readlines())

#read()      读取相应的内容，内容是字节类型
#geturl()    获取请求的url
#getheaders  获取头部信息，列表里面有元组
#getcode()   获取状态码
#readlines   按行读取，返回列表 都是字节类型


'''练习2---下载图片---'''
# image_url = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.ivsky.com%2Fimg%2Fbizhi%2Fpre%2F201511%2F02%2Fzhiyuxi-005.jpg&refer=http%3A%2F%2Fimg.ivsky.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642317431&t=8e2d3fa9725000cf95ef4611621e736b'
# response = urllib.request.urlopen(image_url)
# #图片只能写入本地二进制的格式
# with open('pic.jpg', 'wb') as fp:
#     fp.write(response.read())


'''--------
urllib.parse
    quote     url编码函数，将中文进行转化%xxx
    unquote   url编码函数，将%xxx转化为制定字符
    urlencode 给一个字典，将字典拼接为 query_string，并且实现编码功能
------'''

import urllib.parse

data = {
    'name' : 'name',
    'age'  : 18,
    'city' : 'beijing',
    'height': 220,
}

query_string = urllib.parse.urlencode(data)

print(str(query_string))

